#!/bin/bash

#
# remove different echo command behaviour on different OS
#
if test "`echo -e xxx`" = "xxx"
then
    echo="echo -e"
else
    echo=echo
fi

if [ $# = 0 ]
then
	$echo "\nusage:  runtest ds_system\n"
	$echo "ds_system can be ubuntu1404, python or \"jenkins <dir>\""
	exit 1
fi

#
# test host
#

PYTHON_HOST=controls02
UBUNTU1404_HOST=pcrogucki

HOST=`hostname`
DS_NAME=devTest


case $1 in
ubuntu1404 )
	if [ $HOST != $UBUNTU1404_HOST ]
	then
		$echo "For ubuntu14.04, test should run on "$UBUNTU1404_HOST
		exit 1
	fi
	DIR=../bin
	DEV1="dev/test/10"
	DEV2="dev/test/11"
	DEV3="dev/test/12"
	DEV20="dev/test/20"
	DS_INST_NAME=api
	DS_INST_NAME_2=api2
	DEV1_ALIAS="et_alias"
	DS_PATH=$DIR
	;;
#
python )
	if [ $HOST != $PYTHON_HOST ]
	then
		$echo "For python, test should run on "$PYTHON_HOST
		exit 1
	fi
	DIR=suse93
	;;
#
jenkins )
	if [ $# != 2 ]
	then
		echo "Mising base directory"
		exit -1
	fi
	BASE_DIR=$2
	for i in $NODE_LABELS
	do
		echo $i

		if [ $i = "ubuntu12.04" ]
		then
			echo "Ubuntu 12.04 OS"
			DIR="$BASE_DIR/ubuntu1204/bin"
			DEV1="test/ubuntu1204/10"
			DEV2="test/ubuntu1204/11"
			DEV3="test/ubuntu1204/12"
			DEV1_ALIAS="ubuntu1204_alias"
			DEV20="test/ubuntu1204/20"
			DS_INST_NAME="ubuntu1204"
		fi

		if [ $i = "Windows_VC9" ]
		then
			echo "Windows VC9"
			DIR="$BASE_DIR/win32/bin"
			DEV1="test/win32_vc9/10"
			DEV2="test/win32_vc9/11"
			DEV3="test/win32_vc9/12"
			DEV1_ALIAS="win32_vc9_alias"
			DEV20="test/win32_vc9/20"
			DS_INST_NAME="win32_vc9"
		fi

		if [ $i = "Windows_VC10" ]
		then
			echo "Windows VC10 (64 bits)"
			DIR="$BASE_DIR/win64/bin"
			DEV1="test/win64_vc10/10"
			DEV2="test/win64_vc10/11"
			DEV3="test/win64_vc10/12"
			DEV1_ALIAS="win64_vc10_alias"
			DEV20="test/win64_vc10/20"
			DS_INST_NAME="win64_vc10"
		fi

		if [ $i = "centos5" ]
		then
			echo "CentOS 5 OS"
			DIR="$BASE_DIR/centos5/bin"
			DEV1="test/centos5/10"
			DEV2="test/centos5/11"
			DEV3="test/centos5/12"
			DEV1_ALIAS="centos5_alias"
			DEV20="test/centos5/20"
			DS_INST_NAME="centos5"
		fi

		if [ $i = "redhate4" ]
		then
			echo "Red Hat 4 OS"
			DIR="$BASE_DIR/redhate4/bin"
			DEV1="test/redhate4/10"
			DEV2="test/redhate4/11"
			DEV3="test/redhate4/12"
			DEV1_ALIAS="redhate4_alias"
			DEV20="test/redhate4/20"
			DS_INST_NAME="redhate4"
		fi

		if [ $i = "redhate5" ]
		then
			echo "Red Hat 5 OS"
			DIR="$BASE_DIR/redhate5/bin"
			DEV1="test/redhate5/10"
			DEV2="test/redhate5/11"
			DEV3="test/redhate5/12"
			DEV1_ALIAS="redhate5_alias"
			DEV20="test/redhate5/20"
			DS_INST_NAME="redhate5"
		fi

		if [ $i = "debian6" ]
		then
			echo "Debian 6 OS"
			DIR="$BASE_DIR/debian6/bin"
			DEV1="test/debian6/10"
			DEV2="test/debian6/11"
			DEV3="test/debian6/12"
			DEV1_ALIAS="debian6_alias"
			DEV20="test/debian6/20"
			DS_INST_NAME="debian6"
		fi

		if [ $i = "debian7_tango" ]
		then
			echo "Debian 7 OS"
			DIR="$BASE_DIR/debian7/bin"
			DEV1="test/debian7/10"
			DEV2="test/debian7/11"
			DEV3="test/debian7/12"
			DEV1_ALIAS="debian7_alias"
			DEV20="test/debian7/20"
			DS_INST_NAME="debian7"
		fi

		if [ $i = "debian8_tango" ]
		then
			echo "Debian 8 OS"
			DIR="$BASE_DIR/debian8/bin"
			DEV1="test/debian8/10"
			DEV2="test/debian8/11"
			DEV3="test/debian8/12"
			DEV1_ALIAS="debian_alias"
			DEV20="test/debian8/20"
			DS_INST_NAME="debian8"
		fi

	done
	DS_INST_NAME_2="$DS_INST_NAME"_2
	DS_PATH=$DIR/archive
	;;
#
* )
#	$echo "\nunknown system"
#	exit 1
	DIR=bin
	;;
esac

check_return_value () {
if [ $1 != "0" ]
then
	echo "Test Suite FAILED !!!!!!!!!!!!!!!!!!"
	echo "Leaving test suite, try to fix it"
	$DIR/StopPoll $DEV1
	exit -1
fi
}

#
# First, test device server startup sequence
#

$echo "Testing periodic event"
$DIR/per_event $DEV1 $DEV2
ret=$?
check_return_value $ret
#
$echo "\nTesting miscellaneous attribute data type"
$DIR/att_type_event $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting change event and ApiUtil::cleanup() stuff"
$DIR/change_event $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting 64bits data change event"
$DIR/change_event64 $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting event buffers with change events"
$DIR/change_event_buffer $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting archive_event"
$DIR/archive_event $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting user_event (with DevEncoded data type)"
$DIR/user_event $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting multiple callback on same events"
$DIR/multi_event $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting state and status event"
$DIR/state_event $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting attribute configuration event"
$DIR/att_conf_event $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting event buffers with attribute configuration events"
$DIR/att_conf_event_buffer $DEV1
ret=$?
check_return_value $ret
#
#$echo "\nTesting change events with quality change"
#$DIR/scan $DEV1
#ret=$?
#check_return_value $ret
#
$echo "\nTesting attribute data ready event"
$DIR/data_ready_event $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting event buffers with attribute data ready events"
$DIR/data_ready_event_buffer $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting event from different devices"
$DIR/multi_dev_event $DEV1 $DEV2 $DEV3
ret=$?
check_return_value $ret
#
$echo "\nTesting device interface change event"
$DIR/dev_intr_event $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting pipe event"
$DIR/pipe_event $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting device locking and event usage within the same process (memory pb at process exit)"
$DIR/event_lock $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting server_event"
$DIR/server_event $DEV1 $DEV20
ret=$?
check_return_value $ret
#

#
# kill both servers
#

PIDS=`ps -e | grep $DS_NAME | grep -v grep | awk '{print $1}'`
echo "PIDS = $PIDS"
for pid in $PIDS
do
	echo "Killing process with PID $pid"
	kill $pid
done

sleep 2

#
# Start only one server
#

#$DS_PATH/$DS_NAME $DS_INST_NAME 1>/dev/null 2>&1 &
$DS_PATH/$DS_NAME $DS_INST_NAME &
sleep 20
PIDS=`ps -e | grep $DS_NAME | grep -v grep | awk '{print $1}'`
echo "PIDS = $PIDS"
#
# Next test will restart server
#

$echo "\nTesting event re-connection (takes time...)"
sleep 18 &&  $DS_PATH/$DS_NAME $DS_INST_NAME 1>/dev/null 2>&1 &
sleep 62 &&  $DS_PATH/$DS_NAME $DS_INST_NAME 1>/dev/null 2>&1 &
$DIR/reco_zmq $DEV1 -v
ret=$?
check_return_value $ret

#
# kill servers
#

PIDS=`ps -e | grep $DS_NAME | grep -v grep | awk '{print $1}'`
echo "PIDS = $PIDS"
for pid in $PIDS
do
	echo "Killing process with PID $pid"
	kill $pid
done

sleep 2

#
# Next command will restart server 2
#
PIDS=`ps -e | grep $DS_NAME | grep -v grep`
echo "PIDS = $PIDS"

$echo "\nTesting stateless event connection (takes time...)"
sleep 24 &&  $DS_PATH/$DS_NAME $DS_INST_NAME_2 1>/dev/null 2>&1 &
$DIR/stateless_sub $DEV20 -v
ret=$?
check_return_value $ret

$echo "\n"
#
#$echo "\nTesting client reconnection to notifd (help needed)"
#$DIR/reco_event $DEV1
#ret=$?
#check_return_value $ret
#
#$echo "\nTesting server and client reconnection to notifd (help needed)"
#$DIR/reco_svc $DEV1
#ret=$?
#check_return_value $ret
